package com.muqi.app.pj.shops;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.mine.AddressManageActivity;
import com.muqi.app.qlearn.modles.AddressBean;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;

public class BuyNowActivity extends BaseFragmentActivity implements OnClickListener {
	public static final String GOODS_DETAILS_BEAN = "goods_details_bean";
	private RelativeLayout addressBtn;
	private TextView addrName;
	private TextView addrTels;
	private TextView addrInfo;
	
	private TextView goodsName;
	private TextView unitPrice;
	private ImageView goodsImg;
	private TextView quantityTv;
	private ImageButton minusBtn;
	private ImageButton addBtn;
	
	private TextView totalPrice;
	private Button upThisBtn;
	
	private float utPrice = 0;
	private float totalMoney = 0;
	private int quantity = 1;
	private AddressBean orderAddress = null;
	private ShopDetialBean getData;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getData = (ShopDetialBean)getIntent().getSerializableExtra(GOODS_DETAILS_BEAN);
		if(getData == null){
			finish();
			return;
		}
		setContentView(R.layout.aty_buy_now);
	}

	@Override
	protected void onInit() {
		addressBtn = (RelativeLayout)findViewById(R.id.buy_address_btn);
		addressBtn.setOnClickListener(this);
		addrName = (TextView)findViewById(R.id.buy_address_name);
		addrTels = (TextView)findViewById(R.id.buy_address_telphone);
		addrInfo = (TextView)findViewById(R.id.buy_address_info);
		
		goodsImg = (ImageView)findViewById(R.id.goods_icon);
		goodsName = (TextView)findViewById(R.id.goods_name);
		unitPrice = (TextView)findViewById(R.id.goods_price);
		minusBtn = (ImageButton)findViewById(R.id.goods_minus_btn);
		minusBtn.setOnClickListener(this);
		addBtn = (ImageButton)findViewById(R.id.goods_add_btn);
		addBtn.setOnClickListener(this);
		quantityTv = (TextView)findViewById(R.id.goods_number_tv);
		
		totalPrice = (TextView)findViewById(R.id.goods_total_price);
		upThisBtn = (Button)findViewById(R.id.pay_now_button);
		upThisBtn.setOnClickListener(this);
	}

	@Override
	protected void onData() {
		goodsName.setText(getData.mingcheng);
		unitPrice.setText(getData.danjia+"积分");
		utPrice = Float.parseFloat(getData.danjia);
		if(getData.fileurl!=null){
			Glide.with(this).load(getData.fileurl.fileurl_02)
				.dontAnimate().placeholder(R.drawable.app_logo).into(goodsImg);
		}
		setGoodsPriceView();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buy_address_btn:
			Intent intent=new Intent(this,AddressManageActivity.class);
			startActivityForResult(intent, 1002);
			break;
		case R.id.goods_minus_btn:
			if(quantity > 1){
				quantity = quantity - 1;
				setGoodsPriceView();
			}
			break;
		case R.id.goods_add_btn:
			if(quantity < Integer.parseInt(getData.kucun)){
				quantity = quantity + 1;
				setGoodsPriceView();
			}else{
				ShowToast.showShort(this, "已经没那么多库存了哦");
			}
			break;
		case R.id.pay_now_button:
			if(orderAddress!=null){
				loadingGoodsOrder();
				upThisBtn.setEnabled(false);
			}else{
				ShowToast.showShort(this, "请设置你的收获地址");
			}
			break;
		default:
			break;
		}
	}
	
	private void setGoodsPriceView(){
		quantityTv.setText(quantity+"");
		totalPrice.setText(quantity*utPrice+"积分");
		totalMoney = quantity*utPrice;
	}
	
	private void loadingGoodsOrder() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			String zongji="0.0";
			if(getData.yunfei!=null && !"".equals(getData.yunfei)){
				 zongji=(totalMoney+Float.parseFloat(getData.yunfei))+"";
			}
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("token", mSpUtil.getToken());
			sendMap.put("shangpin_id", getData.shangpin_id);
			sendMap.put("dizhi_id", orderAddress.getDizhi_id());
			sendMap.put("zhifulaiyuan", "credit");
			sendMap.put("danjia", getData.danjia);
			sendMap.put("shuliang", quantity);
			sendMap.put("zongjia", totalMoney);
			sendMap.put("yunfei", getData.yunfei);
			sendMap.put("zongji", totalMoney);
			sendMap.put("liuyan", "");
			sendMap.put("taitou", "");
			String requestUrl = ParamsUtils.buildParams(NetWorkApi.ORDER_WITH_SCORE_GOODS_API, sendMap);
			MyAsyncHttp.get(this, requestUrl, new AsyncCallBackListener() {
				
				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode, String request, String result) {
					if(resuleCode == RESULTCODE.SUCCESS){
						Log.e("===", result);
						OrderIdInfo orderId=ResponseUtils.getOrderId(mContext, result);
						if(orderId!=null){
			
							Intent intent=new Intent(BuyNowActivity.this,OrderIdActivity.class);
							intent.putExtra("orderId", orderId.dingdanhao);
							intent.putExtra("payMoney", orderId.zongji);
							startActivity(intent);
						}
					}
				}
			});
		} else {
			ShowToast.showShort(this, R.string.net_break);// 提示无网络
		}
	}
	

	
	public void onBack(View v){
		this.finish();
	}
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == 1002 && arg1 == RESULT_OK && arg2 != null) {
			orderAddress = (AddressBean) arg2
					.getSerializableExtra("AddressInfo");
			if (orderAddress != null) {
				addrName.setText(orderAddress.getLianxiren());
				addrTels.setText(orderAddress.getLianxidianhua());
				addrInfo.setText(orderAddress.getDizhi());
			}

		}else if(arg0==0 && arg1==RESULT_OK){
			  finish();
		}
	}
}
